package com.jky.trial.controller;

import com.jky.trial.annotation.NoRepeatSubmit;
import com.jky.trial.common.BaseResult;
import com.jky.trial.common.Constants;
import com.jky.trial.common.DateUtil;
import com.jky.trial.common.ListResult;
import com.jky.trial.common.jwt.JWTUtil;
import com.jky.trial.entity.File;
import com.jky.trial.entity.File;
import com.jky.trial.entity.User;
import com.jky.trial.service.FileService;
import com.jky.trial.service.FileService;
import com.jky.trial.vo.FileVo;
import com.jky.trial.vo.ProjectVo;
import com.jky.trial.vo.TaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/file")
public class FileController {

    private FileService recordService;

    @Autowired
    FileController(FileService recordService) {
        this.recordService = recordService;
    }

    @PostMapping

    public BaseResult create(@RequestBody File record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        record.setCreatorId(user.getId());//记录人
        recordService.create(record);
        return new BaseResult(true, record.getId());
    }

    @PutMapping
    public BaseResult update(@RequestBody File record) {
        return new BaseResult(true, recordService.update(record));
    }

    @DeleteMapping("/{id}")
    public BaseResult del(@PathVariable Integer id) {
        return new BaseResult(true, recordService.del(id));
    }

    @GetMapping("/{id}")
    public BaseResult get(@PathVariable Integer id) {
        return new BaseResult(true, recordService.detail(id));
    }

    @GetMapping("/down")
    public OutputStream downByUrl(String url, HttpServletResponse response) {
        try {

            Integer fileId = Integer.parseInt(url.split(",")[0]);
            String path = (url.split(",")[1]);
            File file = recordService.detail(fileId);
            String fileName = URLEncoder.encode(file.getName(), "UTF-8");
            String fileType = "";
            if(file.getName().endsWith(".doc")||file.getName().endsWith(".dox")){
                fileType ="msword";
            }
            if(file.getName().endsWith(".xls")||file.getName().endsWith(".xlsx")){
                fileType ="vnd.ms-excel";
            }
            String contentType="application/"+fileType+";charset=UTF-8";
            response.setContentType(contentType);
            response.setHeader("Content-disposition", "attachment; filename=" + fileName);// 组装附件名称和格式
            response.setCharacterEncoding("UTF-8");
            InputStream is = new URL(path).openStream();
            int c;
            PrintWriter writer = response.getWriter();
            while((c=is.read())!=-1){
                writer.write(c);
            }
            response.flushBuffer();
            is.close();;
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping
    public ListResult page(FileVo record) {
        return recordService.page(record);
    }

    @GetMapping("/list")
    public ListResult list(FileVo record) {
        return new ListResult(true, recordService.list(record));
    }
}
